import sys
from xml.dom import minidom

class config:
	def __init__(self, fanhoso_metadata):
		self.config = {}
		self.loadConfig()
		self.loadNetworkConfig()
		self.config["fanhoso"] = fanhoso_metadata
	
	def getConfig(self):
		return self.config

	def loadConfig(self):
		cfg = self.config

		# Configuration file always resides in "config/config.xml"
		config = open("config/config.xml")
		doc = minidom.parse(config)

		# DTD (fanhoso-config)
		if doc.firstChild.nodeName != "fanhoso-config":
			return False
			# <config>
		if doc.firstChild.nextSibling.nodeName != "config":
			return False
	
		for element in doc.firstChild.nextSibling.childNodes:
			if element.nodeName in ("nickname", "email", "realname"):
				cfg[element.nodeName] = element.firstChild.nodeValue
			if element.nodeName == "network":
				cfg["network"] = {}
				for network in element.childNodes:
					if network.nodeName in ("name", "password", "SSL", "reconnect", "nick-password"):
						if network.hasChildNodes():
							cfg["network"][network.nodeName] = network.firstChild.nodeValue

					if network.nodeName == "channels":
						cfg["network"]["channels"] = []
						for channel in network.childNodes:
							if channel.hasChildNodes():
								cfg["network"]["channels"].append(channel.firstChild.nodeValue)
				
					if network.nodeName == "users":
						cfg["network"]["users"] = {}
						for users in network.childNodes:
							ident = ""
							if users.hasAttributes():
								ident = users.attributes["ident"].value
								cfg["network"]["users"][ident] = {}

							for user in users.childNodes:
								if user.nodeName in ("level", "password"):
									cfg["network"]["users"][ident][user.nodeName] = user.firstChild.nodeValue
						
	def loadNetworkConfig(self):
		cfg = {}
		network = self.config["network"]["name"]

		# Configuration file always resides in "networks/(network)/(network).xml"
		config = open("networks/" + network + "/" + network + ".xml")
		if not config:
			self.parent.debug.error("Couldn't Load network file (%s)", "networks/" + self.config["network"] + "/" + self.config["network"] + ".xml")
			return False

		doc = minidom.parse(config)

		# DTD (fanhoso-network)
		if doc.firstChild.nodeName != "fanhoso-network":
			return False
		# <config>
		if doc.firstChild.nextSibling.nodeName != "network":
			return False
	
		for element in doc.firstChild.nextSibling.childNodes:
			if element.nodeName == "info":
				cfg["info"] = {}
				cfg["info"]["services"] = []
				for info in element.childNodes:
					if info.nodeName in ("name", "ircd"):
						if info.hasChildNodes():
							cfg["info"][info.nodeName] = info.firstChild.nodeValue
					if info.nodeName == "service":
						if info.hasChildNodes():
							cfg["info"]["services"].append(info.firstChild.nodeValue)

			if element.nodeName == "modes":
				cfg["modes"] = {}
				for modes in element.childNodes:
					if modes.nodeName == "user":
						cfg["modes"]["user"] = {}
						if modes.hasChildNodes():
							for usermodes in modes.childNodes:
								if usermodes.nodeName == "modes":
									if usermodes.hasChildNodes():
										cfg["modes"]["user"][usermodes.nodeName] = usermodes.firstChild.nodeValue

								if usermodes.nodeName == "mode":
									if usermodes.hasChildNodes() and usermodes.hasAttributes():
										cfg["modes"]["user"][usermodes.attributes["id"].value] = usermodes.firstChild.nodeValue

					if modes.nodeName == "channel":
						cfg["modes"]["channel"] = {}
						if modes.hasChildNodes():
							for chanmodes in modes.childNodes:
								if chanmodes.nodeName == "modes":
									if chanmodes.hasChildNodes():
										cfg["modes"]["channel"][chanmodes.nodeName] = chanmodes.firstChild.nodeValue

								if chanmodes.nodeName == "modes-double":
									if chanmodes.hasChildNodes():
										cfg["modes"]["channel"][chanmodes.nodeName] = chanmodes.firstChild.nodeValue

								if chanmodes.nodeName == "modes-users":
									if chanmodes.hasChildNodes():
										cfg["modes"]["channel"][chanmodes.nodeName] = chanmodes.firstChild.nodeValue

								if chanmodes.nodeName == "mode":
									if chanmodes.hasChildNodes() and chanmodes.hasAttributes():
										cfg["modes"]["channel"][chanmodes.attributes["id"].value] = chanmodes.firstChild.nodeValue
	
			if element.nodeName == "servers":
				cfg["servers"] = {}
				i = int(0)
				for server in element.childNodes:
					if server.nodeName == "timeout":
						if server.hasChildNodes():
							cfg["servers"][server.nodeName] = server.firstChild.nodeValue

					if server.nodeName == "server":
						cfg["servers"][i] = {}
						if server.hasChildNodes():
							for data in server.childNodes:
								if data.nodeName in ("name", "host", "port"):
									if data.hasChildNodes():
										cfg["servers"][i][data.nodeName] = data.firstChild.nodeValue
						i += 1

		self.config["networkinfo"] = cfg	
